package com.ruoyi.backend.service.Impl;

import com.ruoyi.backend.dto.RestorationDataUploadDTO;
import com.ruoyi.backend.mapper.RestorationDataMapper;
import com.ruoyi.backend.pojo.RestorationData;
import com.ruoyi.backend.service.RestorationDataService;
import com.ruoyi.backend.util.CommmonFileUtils;
import com.ruoyi.backend.util.MinioUtil;
import com.ruoyi.backend.vo.RestorationDataVO;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.backendEnum.RestorationDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Service
public class RestorationDataServiceImpl implements RestorationDataService {



    @Autowired
    private RestorationDataMapper restorationDataMapper;

    @Autowired
    private MinioUtil minioUtil;

    @Value("${minio-bucket.restorationfile}")
    private String minioBucketName;

    @Override
    public void upload(RestorationDataUploadDTO restorationDataUploadDTO) throws IOException {
        //文件存储：上传到minio
        MultipartFile file = restorationDataUploadDTO.getFile();

        // 原始文件名为前端传递的修复报告名称
        String originalFileName=restorationDataUploadDTO.getReportName();
        //minio文件名为系统时分秒+报告名
        String minioFileName = CommmonFileUtils.getMinioFileName(originalFileName);
        restorationDataUploadDTO.setMinioFileName(minioFileName);
        restorationDataUploadDTO.setMinioBucketName(minioBucketName);
        //调用封装的Minio工具类来上传生态修复工程数据
        minioUtil.uploadFile(minioBucketName, minioFileName, file);

        restorationDataMapper.upload(restorationDataUploadDTO);
    }

    @Override
    public void delete(Integer id) {
        if (restorationDataMapper.getRestorationCount(id) == 0){
            throw new ServiceException(RestorationDataException.RESTORATION_DATA_NOT_EXIST);
        }
        //先查询要删除的生态修复工程数据
        RestorationData restorationData = restorationDataMapper.findById(id);
        if (restorationData != null) {
            String minioFileName = restorationData.getMinioFileName();
            // 删除 Minio 中指定桶的文件
            boolean removed = minioUtil.removeFile(minioBucketName, minioFileName);
            if (!removed) {
                throw new ServiceException("删除 Minio 文件失败: " + minioFileName);
            }
        }
        restorationDataMapper.delete(id);
    }

    @Override
    public void update(RestorationData restorationData) {
        if (restorationDataMapper.getRestorationCount(restorationData.getId()) == 0){
            throw new ServiceException(RestorationDataException.RESTORATION_DATA_NOT_EXIST);
        }
        restorationDataMapper.update(restorationData);
    }

    @Override
    public List<RestorationDataVO> search(Integer restorationId, String reportType, LocalDate startTime, LocalDate endTime, String fileType) {
        List<RestorationDataVO> restorationDataList = restorationDataMapper.search(restorationId, reportType, startTime, endTime, fileType);
//        if (restorationDataList.isEmpty()){
//            throw new ServiceException(RestorationDataException.RESTORATION_DATA_NOT_FOUND);
//        }
        return restorationDataList;
    }

    @Override
    public List<RestorationDataVO> searchAll() {
        return restorationDataMapper.searchAll();
    }
}
